using System;
using System.Collections.Generic;
using System.Windows.Forms;
using FileSystemView.SelectedFsElements;
using HogoLogic.HgFileSystem;

namespace FileSystemView
{
	partial class HogoFileView : UserControl
	{
		readonly HogoFileListItemVisualizer visualizer = new HogoFileListItemVisualizer();
		private HgDirectoryInfo currentDirectory;

		public event EventHandler OnSelectedFileChanged;
		
		public HogoFileView()
		{
			InitializeComponent();
		}
		
		public void Open(HgDirectoryInfo directoryInfo)
		{
			currentDirectory = directoryInfo;
			Reload();
		}

		public SelectedFsElement SelectedFsElement
		{
			get
			{
				if (innerListView.SelectedItems.Count == 0)
					return new SelectedNothing();

				List<HgFileInfo> fileInfos = new List<HgFileInfo>();
				foreach (ListViewItem listViewItem in innerListView.SelectedItems)
				{
					HgFileInfo curFile = visualizer.GetFileForListViewItem(listViewItem);
					fileInfos.Add(curFile);
				}

				if (fileInfos.Count == 1)
					return new SelectedFile(fileInfos[0]);
				else
					return new SelectedFileList(fileInfos);
			}
		}

		public ContextMenuStrip InnerContextMenuStrip
		{
			get
			{
				return innerListView.ContextMenuStrip;
			}
			set
			{
				innerListView.ContextMenuStrip = value;
			}
		}

		public GetFilesOptions Options
		{
			get
			{
				return new GetFilesOptions(showUnchangedButton.Checked,
					showUnversionedButton.Checked,
					showIgnoredButton.Checked,
					showFlatButton.Checked
					);
			}
			set
			{
				showUnchangedButton.Checked = value.ShowUnchanged;
				showUnversionedButton.Checked = value.ShowUnversioned;
				showIgnoredButton.Checked = value.ShowIgnored;
				showFlatButton.Checked = value.ShowFlat;
			}
		}

		private void Reload()
		{
			innerListView.Items.Clear();
			innerListView.Items.AddRange(visualizer.MakeListViewItemArray(currentDirectory, Options));
		}

		private void innerListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
		{
			if (innerListView.SelectedItems.Count > 0)
				if (OnSelectedFileChanged != null)
					OnSelectedFileChanged(sender, new EventArgs());
		}

		private void optionButton_Click(object sender, EventArgs e)
		{
			Reload();
		}
	}
}
